import Mock from 'mockjs';

const userList = Mock.mock({
  'list|200': [
    {
      'id|+1': 1,
      'username': '@cname', // 更改为中文名
      'gender|1': ['男', '女'],
      'age|18-60': 18,
      'address': '@county(true)',
      'isDisabled|1': [true, false],
      'registerTime': '@datetime',
      'phone': /^1[3-9]\d{9}$/, // 添加手机号
    },
  ],
});
let total
let filteredList = userList.list;
function getUserListByPage(params) {
  const { page, pageSize, inputSeaech,selectValue } = params;
  if (inputSeaech) {
    filteredList =  userList.list.filter(item => item.username.includes(inputSeaech))
  }
  if(selectValue){
    filteredList =  userList.list.filter(item => item.gender === selectValue)
  }

  if(inputSeaech && selectValue){
    filteredList =  userList.list.filter(item => item.gender === selectValue && item.username.includes(inputSeaech))
  }

  total = filteredList.length
  if (page && pageSize) {
    const start = (page - 1) * pageSize;
    const end = page * pageSize;
    return  filteredList.slice(start, end)
  }
}

Mock.mock('/user/list', 'post', (options) => {
  const { page, pageSize,inputSeaech,selectValue } = JSON.parse(options.body);
  return {
    code: 200,
    list: getUserListByPage({ page, pageSize,inputSeaech,selectValue }),
    total
  }
 
});

// 添加用户
Mock.mock('/user/add', 'post', (options) => {
  const { username, gender, age, address, isDisabled, phone } = JSON.parse(options.body);
  filteredList.unshift({
    id: filteredList.length + 1,
    username,
    gender,
    age,
    address,
    isDisabled,
    registerTime: new Date().toLocaleString(),
    phone,
  });
})
// 删除用户
Mock.mock('/user/delete', 'post', (options) => {
  const { id } = JSON.parse(options.body);
  filteredList = filteredList.filter(item => item.id !== id)
})
// 修改用户
Mock.mock('/user/edit', 'post', (options) => {
  const { id, username, gender, age, address, isDisabled, phone } = JSON.parse(options.body);
  filteredList = filteredList.map(item => {
    if (item.id === id) {
      return {
        ...item,
        username,
        gender,
        age,
        address,
        isDisabled,
        phone,
      }
    }
    return item
  })
})
//是否禁用
Mock.mock('/user/isDisabled', 'post', (options) => {
  const { id, isDisabled } = JSON.parse(options.body);
  filteredList = filteredList.map(item => {
    if (item.id === id) {
      return {
        ...item,
        isDisabled,
      }
    }
    return item
  })
})
//重置
Mock.mock('/user/reset', 'post', (options) => {
  filteredList=userList.list
  const { page, pageSize} = JSON.parse(options.body);
  if (page && pageSize) {
    const start = (page - 1) * pageSize;
    const end = page * pageSize;
    return {
      code: 200,
      msg: '重置成功',
      list: userList.list.slice(start, end),
      total: userList.list.length
    }
  }
})
//批量删除
Mock.mock('/user/batchDelete', 'post', (options) => {
  const { ids } = JSON.parse(options.body);
  filteredList = filteredList.filter(item => !ids.includes(item.id))
  return {
    code: 200,
    msg: '批量删除成功',
    list: filteredList,
    total: filteredList.length
  }
})